<?php
namespace backend\components\helper;

use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;

/**
 * Class Menu
 * @property bool $activeIndex
 * @package backend\components\helper
 */
class Menu extends \dmstr\widgets\Menu
{

    public $activeIndex = false;

    /**
     * @var string
     */
    public static $defaultIcon = 'dot-circle-o';

    private $noDefaultAction;
    private $noDefaultRoute;

    /**
     * format
     * @param $menu
     * @return array
     */
    public static function format($menu){
        $data = json_decode($menu['data'], true);
        $items = $menu['children'];
        $return = [
            'label' => $menu['name'],
            'url' => [$menu['route']],
        ];
        //处理我们的配置
        if ($data) {
            //visible
            isset($data['visible']) && $return['visible'] = $data['visible'];
            //icon
            if(isset($data['icon']) && $data['icon']){
                $return['icon'] = $data['icon'];
                unset($data['icon']);
            }
            //other attribute e.g. class...
            $return['options'] = $data;
        }
        //没配置图标的显示默认图标，默认图标大家可以自己随便修改
        $return['icon'] = $return['icon']??self::$defaultIcon;

        $items && $return['items'] = $items;

        return $return;
    }

    /**
     * Renders the menu.
     */
    public function run()
    {
        if ($this->route === null && Yii::$app->controller !== null) {
            $this->route = Yii::$app->controller->getRoute();
        }
        if ($this->params === null) {
            $this->params = Yii::$app->request->getQueryParams();
        }
        $posDefaultAction = strpos($this->route, Yii::$app->controller->defaultAction);
        if ($posDefaultAction) {
            $this->noDefaultAction = rtrim(substr($this->route, 0, $posDefaultAction), '/');
        } else {
            $this->noDefaultAction = false;
        }
        $posDefaultRoute = strpos($this->route, Yii::$app->controller->module->defaultRoute);
        if ($posDefaultRoute) {
            $this->noDefaultRoute = rtrim(substr($this->route, 0, $posDefaultRoute), '/');
        } else {
            $this->noDefaultRoute = false;
        }
        $items = $this->normalizeItems($this->items, $hasActiveChild);
        if (!empty($items)) {
            $options = $this->options;
            $tag = ArrayHelper::remove($options, 'tag', 'ul');

            return Html::tag($tag, $this->renderItems($items), $options);
        }
    }

    /**
     * @inheritdoc
     */
    protected function normalizeItems($items, &$active)
    {
        foreach ($items as $i => $item) {
            if (isset($item['visible']) && !$item['visible']) {
                unset($items[$i]);
                continue;
            }
            if (!isset($item['label'])) {
                $item['label'] = '';
            }
            $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
            $items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label'];
            $items[$i]['icon'] = isset($item['icon']) ? $item['icon'] : '';
            $hasActiveChild = false;
            if (isset($item['items'])) {
                $items[$i]['items'] = $this->normalizeItems($item['items'], $hasActiveChild);
                if (empty($items[$i]['items']) && $this->hideEmptyItems) {
                    unset($items[$i]['items']);
                    if (!isset($item['url'])) {
                        unset($items[$i]);
                        continue;
                    }
                }
            }
            if (!isset($item['active'])) {
                if ($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item)) {
                    $active = $items[$i]['active'] = true;
                    $this->activeIndex = true;
                } else {
                    $items[$i]['active'] = false;
                }
            } elseif ($item['active']) {
                $active = true;
            }
        }
        return array_values($items);
    }

    /**
     * getMenuInfo
     * @param $allMenuList
     * @return array
     */
    public static function getMenuInfo($allMenuList)
    {
        $topMenusHtml = '';
        $leftMenuDivHtml = '';

        foreach ($allMenuList as $index => $menu){
            if (!($menu['visible']??1)  || empty($menu['items']) ) continue;

            $widget = Yii::createObject([
                'class'=> self::class,
                'options' => ['class' => 'sidebar-menu tree', 'data-widget'=> 'tree'],
                'items'  => $menu['items'],
            ]);

            $leftMenuHtml = $widget->run();

            $isActive = $widget->activeIndex?'active':'';

            $topMenusHtml .= Html::tag('li','<a href="javascript:;"><i class="fa fa-'.$menu['icon'].'"></i> '.$menu['label'].'</a>',[
                'class' => $isActive,
                'data-id' => 'module-menu-'.$index,
            ]);

            $leftMenuDivHtml .= Html::tag('div',$leftMenuHtml,[
                'id' => 'module-menu-'.$index,
                'class' => $isActive?:'hide',
            ]);
        }
        return [Html::tag('ul',$topMenusHtml,[
            'class' => 'nav navbar-nav'
        ]),$leftMenuDivHtml];
    }

}
